package org.usp.poli.bipt.client;

import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import org.usp.poli.bipt.client.chart.Chart;
import org.usp.poli.bipt.client.metadata.MetaData;
import org.usp.poli.bipt.client.ui.*;
import org.usp.poli.bipt.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.PieChart;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BrazilianIPT implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	public static final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	public static HorizontalPanel mainareaHPnl;
	public static MetaDataMenu metadataMenu;
	private static MetaDataForm metadataForm;
	
	public static String keyBD = null;
	public static Integer fluxo = null;
	public static Integer formComplete = 0;
	static GenericDialog genericDialog;
	public static Boolean isLogin = false;
	
	
	public static final Menu moduleMenu = new Menu();
	public static final Banner moduleBanner = new Banner();
	public static final Footer moduleFooter = new Footer();
	public static final Home moduleHome = new Home();
	public static final VerticalPanel mainVPnl = new VerticalPanel();
	
	
	private MetaData metaData = new MetaData();
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		final Menu moduleMenu = new Menu();
		final Banner moduleBanner = new Banner();
		final Footer moduleFooter = new Footer();
		final Home moduleHome = new Home();
		final VerticalPanel mainVPnl = new VerticalPanel();
		metadataMenu = new MetaDataMenu();
		
		
		mainareaHPnl = new HorizontalPanel();
		mainVPnl.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		
		this.setMenuLogout();
//		mainVPnl.add(moduleBanner.getBanner());
//		mainVPnl.add(moduleMenu.getMenuLogout());
//		mainVPnl.add(moduleHome.getHome());
//		mainVPnl.add(moduleFooter.getFooter());
//		RootPanel.get("MainPnl").add(mainVPnl);
		
				    
	    metadataForm = new MetaDataForm();
		
		//Events
		
		
		
		
		
		
		
		
		((PushButton) metadataMenu.getBasicmetadataLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getBasicMetaData());
	    	}
	    });
		
		((PushButton) metadataMenu.getGeneralKeywordsLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getGeneralKeywords());
	    	}
	    });
		
		((PushButton) metadataMenu.getGeographicCoverageLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getGeographicCoverage());
	    	}
	    });
		
		((PushButton) metadataMenu.getIpRightsLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getIPRights());
	    	}
	    });
		
		((PushButton) metadataMenu.getLowestCommonTaxLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getLowestCommonTax());
	    	}
	    });
		
		((PushButton) metadataMenu.getMethodsLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getMethods());
	    	}
	    });
		
		((PushButton) metadataMenu.getResearchProjectLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getResearchProject());
	    	}
	    });
		
		((PushButton) metadataMenu.getResourceOriginatorLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getResourceOriginator());
	    	}
	    });
		
		((PushButton) metadataMenu.getTaxonomicCoverageLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getTaxonomicCoverage());
	    	}
	    });
		

		((PushButton) metadataMenu.getTemporalCoverageLk()).addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent sender) {
	    		mainareaHPnl.clear();

	    		mainareaHPnl.add(metadataMenu.getMenuForm());
	    		mainareaHPnl.add(metadataForm.getTemporalCoverage());
	    	}
	    });
		
		
		
//		((PushButton) metadataForm.getBasicMetadataBtn()).addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				FlexTable basicMetadataFlex;
//				basicMetadataFlex = (FlexTable) metadataForm.getBasicMetaData();
//				
//				basicMetadataBind((TextBox)basicMetadataFlex.getWidget(1, 1), (TextBox)basicMetadataFlex.getWidget(2, 1), (TextBox)basicMetadataFlex.getWidget(3, 1),
//						(TextBox)basicMetadataFlex.getWidget(4, 1), (TextBox)basicMetadataFlex.getWidget(5, 1));
//				
//				
//
//				String teste;
//				teste = metaData.getTitle();
//				
//			}
//			public void basicMetadataBind(final TextBox titleTB,
//					final TextBox contactNameTB, final TextBox contactEmailTB,
//					final TextBox uploadLogoTB, final TextBox descriptionTB) {
//				metaData.setTitle(titleTB.getText());
//				metaData.setContactName(contactNameTB.getText());
//				metaData.setContactEmail(contactEmailTB.getText());
//				metaData.setUpdateLogo(uploadLogoTB.getText());
//				metaData.setDescription(descriptionTB.getText());
//			}
//			
//		});
		
		
		
		//Login
		
		
		
		
		
//		((PushButton) newResourceForm.getCadastroBtn()).addClickHandler(new ClickHandler() {
//	    	public void onClick(ClickEvent sender) {
//	    		//DarwinCoreBasic newResource = new DarwinCoreBasic();
//	    		
//	    	}
//	    });
		
		
		
		
	}
	static public void setResourceConfig(){
		mainareaHPnl.clear();

		mainareaHPnl.add(metadataMenu.getMenuForm());
		mainareaHPnl.add(metadataForm.getBasicMetaData());
		BrazilianIPT.metadataMenu.getGeneralKeywordsLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getGeographicCoverageLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getIpRightsLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getLowestCommonTaxLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getMethodsLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getResearchProjectLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getTemporalCoverageLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getTaxonomicCoverageLk().setEnabled(false);
		BrazilianIPT.metadataMenu.getResourceOriginatorLk().setEnabled(false);
		
	}
	
	static public void setMenuLogin(){
		mainVPnl.clear();
		mainVPnl.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		mainVPnl.add(moduleBanner.getBanner());
		mainVPnl.add(moduleMenu.getMenuLogin());
		mainareaHPnl.clear();
		mainareaHPnl.add(moduleHome.getHome());
		mainVPnl.add(mainareaHPnl);
		mainVPnl.add(moduleFooter.getFooter());
		//RootPanel.get().clear();
		RootPanel.get("MainPnl").add(mainVPnl);
		
		
	}
	static public void setMenuLogout(){
		
		mainVPnl.clear();
		mainVPnl.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		mainVPnl.add(moduleBanner.getBanner());
		mainVPnl.add(moduleMenu.getMenuLogout());
		mainareaHPnl.clear();
		mainareaHPnl.add(moduleHome.getHome());
		mainVPnl.add(mainareaHPnl);
		mainVPnl.add(moduleFooter.getFooter());
		//RootPanel.get().clear();
		RootPanel.get("MainPnl").add(mainVPnl);
		
	}
	static public void showNextPanel(){
		mainareaHPnl.clear();
		mainareaHPnl.add(metadataMenu.getMenuForm());
		
		switch(fluxo){
			case 1:
				mainareaHPnl.add(metadataForm.getGeographicCoverage());
				break;
			case 2:
				mainareaHPnl.add(metadataForm.getLowestCommonTax());
				break;
			case 3:
				mainareaHPnl.add(metadataForm.getResearchProject());
				break;
			case 4:
				mainareaHPnl.add(metadataForm.getMethods());
				break;
			case 5:
				mainareaHPnl.add(metadataForm.getResourceOriginator());
				break;
			case 6:
				mainareaHPnl.add(metadataForm.getTaxonomicCoverage());
				break;
			case 7:
				mainareaHPnl.add(metadataForm.getIPRights());
				break;
			case 8:
				mainareaHPnl.add(metadataForm.getGeneralKeywords());
				break;
			case 9:
				mainareaHPnl.add(metadataForm.getTemporalCoverage());
				break;
			case 10:
				mainareaHPnl.clear();
				
				genericDialog = new GenericDialog("Concluído", "Metadados incluídos com sucesso!!!",false);
				((DialogBox) genericDialog.getDialog()).center();
				((DialogBox) genericDialog.getDialog()).show();
				
				((Button)genericDialog.getBotaoOk()).addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						((DialogBox) genericDialog.getDialog()).hide();
					}
				});
				break;
		}
			
		
		
	}
	public static void setHome() {
		mainareaHPnl.clear();
		mainareaHPnl.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		mainareaHPnl.add(moduleHome.getHome());
		
	}
	public static void callCollection(String key) {
		mainareaHPnl.clear();
		mainareaHPnl.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		Explore explore = new Explore();
		mainareaHPnl.add(explore.getExplore());
		
	}
	
	private static AbstractDataTable createTable() {
	    DataTable data = DataTable.create();
	    data.addColumn(ColumnType.STRING, "Task");
	    data.addColumn(ColumnType.NUMBER, "Hours per Day");
	    data.addRows(2);
	    data.setValue(0, 0, "Work");
	    data.setValue(0, 1, 14);
	    data.setValue(1, 0, "Sleep");
	    data.setValue(1, 1, 10);
	    return data;
	  }
}
